﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TinyEngine.Core.Parsers;
using System.Xml.Linq;
using TinyEngine.Core.Asset;

namespace TinyEngine.Core.Entities.Parsers
{
    public class SpriteParser : TinyEngine.Core.Parsers.IEntityParser
    {
        public System.Xml.Linq.XElement ToXmlElement(IEntity obj)
        {
            var root = new XElement(obj.GetType().Name);
            var sprite = obj as SpriteEntity;

            ParserHelper.ParseIEntityToXml(obj, root);

            root.SetAttributeValue("Layer", ParserHelper.SingleToString(sprite.Layer));

            ParserHelper.ParseAssetHolderToXML<SpriteAsset>(obj, root);

            return root;
        }

        public IEntity FromXmlElement(System.Xml.Linq.XElement element)
        {
            var sp = new SpriteEntity();
            ParserHelper.ParseXmlToIEntity(element, sp);
            sp.Layer = ParserHelper.TryParseSingle("Layer", element);
            return sp;
        }
    }
}
